﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Rapid
{
    public partial class frmShowResults : frmMenustrip
    {
        Dictionary<ETest, Label> dicLabelTestName;
        Dictionary<ETest, Label> dicLabelObservation;
        Dictionary<ETest, Label> dicLabelDrugLevel;
        Dictionary<ETest, Label> dicLabelInterpretation;

        Dictionary<ETestCommon, Label> dicLabelCommonTestName;
        Dictionary<ETestCommon, Label> dicLabelCommonObservation;
        Dictionary<ETestCommon, Label> dicLabelCommonDrugLevel;
        Dictionary<ETestCommon, Label> dicLabelCommonInterpretation;

        //Dictionary<ETest, Panel> dicRows;

        RapidDataDataContext db = new RapidDataDataContext();

        public frmShowResults()
        {
            InitializeComponent();

            #region style for button
            btnCancel.TabStop = false;
            btnCancel.FlatStyle = FlatStyle.Flat;
            btnCancel.FlatAppearance.BorderSize = 0;
            btnCancel.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); //Transparent

            btnPrint.TabStop = false;
            btnPrint.FlatStyle = FlatStyle.Flat;
            btnPrint.FlatAppearance.BorderSize = 0;
            btnPrint.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); //Transparent

            btnExport.TabStop = false;
            btnExport.FlatStyle = FlatStyle.Flat;
            btnExport.FlatAppearance.BorderSize = 0;
            btnExport.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); //Transparent
            #endregion

            #region labels test name
            dicLabelTestName = new Dictionary<ETest, Label>()
            {
                {ETest.AMP, lblAMP},
                {ETest.BAR, lblBAR},

                {ETest.BZO, lblBZO},
                {ETest.COC, lblCOC},

                {ETest.MDMA, lblMDMA},
                {ETest.MET, lblMET},

                {ETest.MTD, lblMTD},
                {ETest.OPI, lblOPI},

                {ETest.OXY, lblOXY},
                {ETest.PCP, lblPCP},

                {ETest.TCA, lblTCA},
                {ETest.THC, lblTHC}
            };
            #endregion


            #region labels for Observation Test
            dicLabelObservation = new Dictionary<ETest, Label>()
            {
                {ETest.AMP, lbl_AMP_Obser},
                {ETest.BAR, lbl_BAR_Obser},

                {ETest.BZO, lbl_BZO_Obser},
                {ETest.COC, lbl_COC_Obser},

                {ETest.MDMA, lbl_MDMA_Obser},
                {ETest.MET, lbl_MET_Obser},

                {ETest.MTD, lbl_MTD_Obser},
                {ETest.OPI, lbl_OPI_Obser},

                {ETest.OXY, lbl_OXY_Obser},
                {ETest.PCP, lbl_PCP_Obser},

                {ETest.TCA, lbl_TCA_Obser},
                {ETest.THC, lbl_THC_Obser}
            };
            #endregion

            #region labels for Drug Level Test
            dicLabelDrugLevel = new Dictionary<ETest, Label>()
            {
                {ETest.AMP, lbl_AMP_Drug},
                {ETest.BAR, lbl_BAR_Drug},

                {ETest.BZO, lbl_BZO_Drug},
                {ETest.COC, lbl_COC_Drug},

                {ETest.MDMA, lbl_MDMA_Drug},
                {ETest.MET, lbl_MET_Drug},

                {ETest.MTD, lbl_MTD_Drug},
                {ETest.OPI, lbl_OPI_Drug},

                {ETest.OXY, lbl_OXY_Drug},
                {ETest.PCP, lbl_PCP_Drug},

                {ETest.TCA, lbl_TCA_Drug},
                {ETest.THC, lbl_THC_Drug}
            };
            #endregion

            #region labels for Interpretation Test
            dicLabelInterpretation = new Dictionary<ETest, Label>()
            {
                {ETest.AMP, lbl_AMP_Inter},
                {ETest.BAR, lbl_BAR_Inter},

                {ETest.BZO, lbl_BZO_Inter},
                {ETest.COC, lbl_COC_Inter},

                {ETest.MDMA, lbl_MDMA_Inter},
                {ETest.MET, lbl_MET_Inter},

                {ETest.MTD, lbl_MTD_Inter},
                {ETest.OPI, lbl_OPI_Inter},

                {ETest.OXY, lbl_OXY_Inter},
                {ETest.PCP, lbl_PCP_Inter},

                {ETest.TCA, lbl_TCA_Inter},
                {ETest.THC, lbl_THC_Inter}
            };
            #endregion


            #region labels for Common Test name
            dicLabelCommonTestName = new Dictionary<ETestCommon, Label>()
            {
                {ETestCommon.Cr, lblCr},
                {ETestCommon.Ni, lblNi},

                {ETestCommon.pH, lblpH},
                {ETestCommon.Bl, lblBl},

                {ETestCommon.SG, lblSG}
            };
            #endregion

            #region labels for Observation Common Test
            dicLabelCommonObservation = new Dictionary<ETestCommon, Label>()
            {
                {ETestCommon.Cr, lbl_Cr_Obser},
                {ETestCommon.Ni, lbl_Ni_Obser},

                {ETestCommon.pH, lbl_pH_Obser},
                {ETestCommon.Bl, lbl_Bl_Obser},

                {ETestCommon.SG, lbl_SG_Obser}
            };
            #endregion

            #region labels for Drug Level Common Test
            dicLabelCommonDrugLevel = new Dictionary<ETestCommon, Label>()
            {
                {ETestCommon.Cr, lbl_Cr_Drug},
                {ETestCommon.Ni, lbl_Ni_Drug},

                {ETestCommon.pH, lbl_pH_Drug},
                {ETestCommon.Bl, lbl_Bl_Drug},

                {ETestCommon.SG, lbl_SG_Drug}
            };
            #endregion


            #region labels for InterPretation Common Test
            dicLabelCommonInterpretation = new Dictionary<ETestCommon, Label>()
            {
                {ETestCommon.Cr, lbl_CR_Inter},
                {ETestCommon.Ni, lbl_Ni_Inter},

                {ETestCommon.pH, lbl_pH_Inter},
                {ETestCommon.Bl, lbl_Bl_Inter},

                {ETestCommon.SG, lbl_SG_Inter}
            };
            #endregion


            #region init labels Test from result pre step
            foreach (var lblObser in dicLabelObservation)
                lblObser.Value.Text = TestResults.DicResults[lblObser.Key].GetObservstionName();

            foreach (var lblDrugLevel in dicLabelDrugLevel)
                lblDrugLevel.Value.Text = TestResults.DicResults[lblDrugLevel.Key].GetDrugLevel();

            foreach (var lblInterpretation in dicLabelInterpretation)
            {
                lblInterpretation.Value.Text = TestResults.DicResults[lblInterpretation.Key].GetInterPretation();
                if (Flag.IsChecked(db, TestResults.DicResults[lblInterpretation.Key].observation))
                    lblInterpretation.Value.ForeColor = Color.Red;
            }

            if (TestResults.isMonitoring)
            {
                lbl_MTD_Inter.Text = TestResults.DicResults[ETest.MTD].GetInterPretationMonitoring();
                lbl_MTD_Inter.ForeColor = (Flag.IsChecked_Monitoring(db, TestResults.DicResults[ETest.MTD].observation)) ? Color.Red : Color.Black;
            }
            #endregion

            #region init labels Common Test from result pre step
            foreach (var commonTest in TestResults.DicCommonResults)
            {
                dicLabelCommonObservation[commonTest.Key].Text = commonTest.Value.GetObservstionName();
                dicLabelCommonInterpretation[commonTest.Key].Text = commonTest.Value.GetInterPretation();
                if (Flag.IsChecked(db, commonTest.Value.observation))
                    dicLabelCommonInterpretation[commonTest.Key].ForeColor = Color.Red;
            }
            #endregion

            #region fire event test hover
            foreach (var labelTestName in dicLabelTestName)
                labelTestName.Value.MouseHover += new EventHandler(labelTestName_MouseHover);

            foreach (var labelObservation in dicLabelObservation)
                labelObservation.Value.MouseHover += new EventHandler(labelObservation_MouseHover);

            foreach (var labelDrug in dicLabelDrugLevel)
                labelDrug.Value.MouseHover += new EventHandler(labelDrug_MouseHover);

            foreach (var labelInterpretation in dicLabelInterpretation)
                labelInterpretation.Value.MouseHover += new EventHandler(labelInterpretation_MouseHover);
            #endregion

            //dicLabelInterpretation
            #region fire event common test hover
            foreach (var labelCommonTestName in dicLabelCommonTestName)
                labelCommonTestName.Value.MouseHover += new EventHandler(labelCommonTestName_MouseHover);

            foreach (var labelCommonObservation in dicLabelCommonObservation)
                labelCommonObservation.Value.MouseHover += new EventHandler(labelCommonObservation_MouseHover);

            foreach (var labelCommonInterpretation in dicLabelCommonInterpretation)
                labelCommonInterpretation.Value.MouseHover += new EventHandler(labelCommonInterpretation_MouseHover);

            foreach (var labelCommonDrugLevel in dicLabelCommonDrugLevel)
                labelCommonDrugLevel.Value.MouseHover += new EventHandler(labelCommonDrugLevel_MouseHover);
            #endregion
            //dicLabelDrugLevel
        }

        #region test label hover

            void labelTestName_MouseHover(object sender, EventArgs e)
            {
                Label labelTestName = sender as Label;
                var test = TestResults.DicResults[dicLabelTestName.Single(r => r.Value == labelTestName).Key];
                toolTip1.SetToolTip(labelTestName, Comment.GetComment(test.observation, test.testType));
            }

            void labelObservation_MouseHover(object sender, EventArgs e)
            {
                Label labelObservation = sender as Label;
                var test = TestResults.DicResults[dicLabelObservation.Single(r => r.Value == labelObservation).Key];
                toolTip1.SetToolTip(labelObservation, Comment.GetComment(test.observation, test.testType));
            }

            void labelDrug_MouseHover(object sender, EventArgs e)
            {
                Label labelDrug = sender as Label;
                var test = TestResults.DicResults[dicLabelDrugLevel.Single(r => r.Value == labelDrug).Key];
                toolTip1.SetToolTip(labelDrug, Comment.GetComment(test.observation, test.testType));
            }

            void labelInterpretation_MouseHover(object sender, EventArgs e)
            {
                Label labelInterpretation = sender as Label;
                var test = TestResults.DicResults[dicLabelInterpretation.Single(r => r.Value == labelInterpretation).Key];
                toolTip1.SetToolTip(labelInterpretation, Comment.GetComment(test.observation, test.testType));
            }
        #endregion

        #region common test label hover

            void labelCommonTestName_MouseHover(object sender, EventArgs e)
            {
                Label labelCommonTestName = sender as Label;
                var test = TestResults.DicCommonResults[dicLabelCommonTestName.Single(r => r.Value == labelCommonTestName).Key];
                toolTip1.SetToolTip(labelCommonTestName, Comment.GetCommonComment(test.observation, test.testType));
            }

            void labelCommonObservation_MouseHover(object sender, EventArgs e)
            {
                Label labelCommonObservation = sender as Label;
                var test = TestResults.DicCommonResults[dicLabelCommonObservation.Single(r => r.Value == labelCommonObservation).Key];
                toolTip1.SetToolTip(labelCommonObservation, Comment.GetCommonComment(test.observation, test.testType));
            }

            void labelCommonDrugLevel_MouseHover(object sender, EventArgs e)
            {
                Label labelCommonDrugLevel = sender as Label;
                var test = TestResults.DicCommonResults[dicLabelCommonDrugLevel.Single(r => r.Value == labelCommonDrugLevel).Key];
                toolTip1.SetToolTip(labelCommonDrugLevel, Comment.GetCommonComment(test.observation, test.testType));
            }

            void labelCommonInterpretation_MouseHover(object sender, EventArgs e)
            {
                Label labelCommonInterpretation = sender as Label;
                var test = TestResults.DicCommonResults[dicLabelCommonInterpretation.Single(r => r.Value == labelCommonInterpretation).Key];
                toolTip1.SetToolTip(labelCommonInterpretation, Comment.GetCommonComment(test.observation, test.testType));
            }

        #endregion

        private void frmShowResults_Load(object sender, EventArgs e)
        {
            txtComment.Text = Comment.GetFlagComments();
            //listviewControlResult. = ControlTestResults.ControlTestList;
            //grvControlResults.DataSource = ControlTestResults.ControlTestList;
        }

        private void lbl_Observation_Test_MouseHover(object sender, EventArgs e)
        {
            Label lbl_Observation_Test = sender as Label;
            lbl_Observation_Test.BackColor = Color.FromArgb(242, 242, 242);
            lbl_Observation_Test.BorderStyle = BorderStyle.FixedSingle;
        }

        private void lbl_Observation_Test_MouseLeave(object sender, EventArgs e)
        {
            Label lbl_Observation_Test = sender as Label;
            lbl_Observation_Test.BackColor = Color.White;
            lbl_Observation_Test.BorderStyle = BorderStyle.None;
        }

        private void lbl_Observation_Test_Click(object sender, EventArgs e)
        {
            Label lbl_Observation_Test = sender as Label;

            // get type test of control clicked
            var key_TypeTest = dicLabelObservation.Single(lblOb => lblOb.Value == lbl_Observation_Test).Key;

            // change Observation
            TestResults.DicResults[key_TypeTest].ChangeObservationWithRules();
            lbl_Observation_Test.Text = TestResults.DicResults[key_TypeTest].GetObservstionName();

            // update text DrugLevel
            dicLabelDrugLevel[key_TypeTest].Text = TestResults.DicResults[key_TypeTest].GetDrugLevel();

            if (key_TypeTest == ETest.MTD && TestResults.isMonitoring)
            {
                // update text InterPretation
                dicLabelInterpretation[key_TypeTest].Text = TestResults.DicResults[key_TypeTest].GetInterPretationMonitoring();

                // change color InterPretation
                dicLabelInterpretation[key_TypeTest].ForeColor =
                    Flag.IsChecked_Monitoring(db, TestResults.DicResults[key_TypeTest].observation) ?
                    Color.Red : Color.Black;

                // change comment
                txtComment.Text = Comment.GetMonitoringComment(TestResults.DicResults[key_TypeTest].observation, key_TypeTest);
            }
            else
            {
                dicLabelInterpretation[key_TypeTest].Text = TestResults.DicResults[key_TypeTest].GetInterPretation();

                // change color InterPretation
                dicLabelInterpretation[key_TypeTest].ForeColor =
                    Flag.IsChecked(db, TestResults.DicResults[key_TypeTest].observation) ?
                    Color.Red : Color.Black;

                toolTip1.SetToolTip(lbl_Observation_Test, Comment.GetComment(TestResults.DicResults[key_TypeTest].observation, key_TypeTest));

                // change comment
                txtComment.Text = Comment.GetFlagComments();
            }
        }

        private void lblObservation_CommonTest_MouseHover(object sender, EventArgs e)
        {
            Label lbl_Observation_Test = sender as Label;
            lbl_Observation_Test.BackColor = Color.FromArgb(242, 242, 242);
            lbl_Observation_Test.BorderStyle = BorderStyle.FixedSingle;
        }

        private void lblObservation_CommonTest_MouseLeave(object sender, EventArgs e)
        {
            Label lbl_Observation_Test = sender as Label;
            lbl_Observation_Test.BackColor = Color.White;
            lbl_Observation_Test.BorderStyle = BorderStyle.None;
        }

        private void lblObservation_CommonTest_Click(object sender, EventArgs e)
        {
            Label lbl_Observation_Test = sender as Label;

            // get type test of control clicked
            var key_TypeTest = dicLabelCommonObservation.Single(lblOb => lblOb.Value == lbl_Observation_Test).Key;

            // change Observation
            TestResults.DicCommonResults[key_TypeTest].ChangeObservationWithRules();
            lbl_Observation_Test.Text = TestResults.DicCommonResults[key_TypeTest].GetObservstionName();

            // update text InterPretation
            dicLabelCommonInterpretation[key_TypeTest].Text = TestResults.DicCommonResults[key_TypeTest].GetInterPretation();


            // change color InterPretation
            //if (dicResultsCommonOriginal[key_TypeTest] != TestResults.DicCommonResults[key_TypeTest].observation)
            if (Flag.IsChecked(db, TestResults.DicCommonResults[key_TypeTest].observation) )
                dicLabelCommonInterpretation[key_TypeTest].ForeColor = Color.Red;
            else
                dicLabelCommonInterpretation[key_TypeTest].ForeColor = Color.Black;

            toolTip1.SetToolTip(lbl_Observation_Test, Comment.GetCommonComment(TestResults.DicCommonResults[key_TypeTest].observation, key_TypeTest));

            // change comment
            //txtComment.Text = Comment.GetCommonComment(TestResults.DicCommonResults[key_TypeTest].observation, key_TypeTest);
            txtComment.Text = Comment.GetFlagComments();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            PatientView.Reset();
            TestResults.Reset();
            this.Hide();
            frmRapidRead _frmRapidRead = new frmRapidRead();
            _frmRapidRead.Show();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (!PatientView.Add())
                return;

            string folderpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Rapid Reads";


            if (!Directory.Exists(folderpath))
                Directory.CreateDirectory(folderpath);

            //Report.ExportPdf(folderpath, GetFlagComments());

            frmPreview _frmPreview = new frmPreview(Report.ExportPdf(folderpath));
            _frmPreview.StartPosition = FormStartPosition.CenterScreen;
            _frmPreview.Show();

            //Libraries\Documents
        }
    }
}